package com.resou.reader.mine.paycenter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.api.entry.PayConfigBean;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class PaymentMethodDialog extends DialogFragment implements View.OnClickListener {
    public static final String PAY_CONFIG_BEAN = "pay_config_bean";

    @BindView(R.id.iv_back)
    ImageView ivBack;
    Unbinder unbinder;
    @BindView(R.id.ll_we_chat)
    LinearLayout llWeChat;
    @BindView(R.id.ll_ali_pay)
    LinearLayout llAliPay;
    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.tv_currency)
    TextView tvCurrency;
    @BindView(R.id.tv_voucher)
    TextView tvVoucher;
    private PayConfigBean mPayConfigBean;

    public static PaymentMethodDialog newInstance(PayConfigBean bean) {
        Bundle args = new Bundle();
        args.putParcelable(PAY_CONFIG_BEAN, bean);
        PaymentMethodDialog fragment = new PaymentMethodDialog();
        fragment.setArguments(args);
        return fragment;
    }

    Activity mActivity;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (Activity) context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mPayConfigBean = getArguments().getParcelable(PAY_CONFIG_BEAN);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.dialog_payment_method, container, false);
        unbinder = ButterKnife.bind(this, view);

        //人民币金额
        tvAmount.setText("¥"+ (int) mPayConfigBean.getCnyPrice());
        //热币数量
        tvCurrency.setText(String.valueOf((int) mPayConfigBean.getBookBean() + getString(R.string.currency)));
        //热券数量
        if (mPayConfigBean.getGiftBean() > 0) {
            tvVoucher.setText("赠" + String.valueOf((int) mPayConfigBean.getGiftBean() + "热券"));
        } else {
            tvVoucher.setVisibility(View.GONE);
        }

        ivBack.setOnClickListener(this);
        llAliPay.setOnClickListener(this);
        llWeChat.setOnClickListener(this);

        return view;
    }

    private static final String TAG = "ThreeDaysRechargeDialog";

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.d(TAG, "onActivityCreated: ");
        getDialog().setCanceledOnTouchOutside(false);
        Window win = getDialog().getWindow();
        DisplayMetrics dm = new DisplayMetrics();
        // 一定要设置Background，如果不设置，window属性设置无效
        if (win != null) {
            win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//        win.setLayout((dm.widthPixels), (int) (dm.heightPixels * 0.5));
            mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
            WindowManager.LayoutParams params = win.getAttributes();
            params.windowAnimations = R.style.bottomSheet_animation;
            params.gravity = Gravity.BOTTOM;
            params.width = ViewGroup.LayoutParams.MATCH_PARENT;
            win.setAttributes(params);
            // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
//        params.height = (int) (dm.heightPixels * 0.5);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onClick(View v) {
        if (v == ivBack) {
            dismiss();
        }
        if (v == llAliPay && mInteractionListener != null) {
            mInteractionListener.onAliPayClicked(mPayConfigBean);
        }
        if (v == llWeChat && mInteractionListener != null) {
            mInteractionListener.onWeChatClicked(mPayConfigBean);
        }
    }

    OnPaymentDialogInteractionListener mInteractionListener;

    public void setInteractionListener(OnPaymentDialogInteractionListener interactionListener) {
        mInteractionListener = interactionListener;
    }

    public interface OnPaymentDialogInteractionListener {
        void onAliPayClicked(PayConfigBean payConfigBean);

        void onWeChatClicked(PayConfigBean payConfigBean);
    }
}
